package net.ksic.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class NetworkManager {

	public String loginJSP(String url, String msg) {
		String result = "";
		
		Log.i("url", url);

		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);

		try {
			String msgdata[] = msg.split(",");

			Log.i("msgdata[]", msgdata[0] + "," + msgdata[1]);

			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			
			nameValuePairs.add(new BasicNameValuePair("id", msgdata[0]));
			nameValuePairs.add(new BasicNameValuePair("pw", msgdata[1]));
			
			post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			HttpResponse response = client.execute(post);

			BufferedReader bufreader = new BufferedReader(
					new InputStreamReader(response.getEntity().getContent(),
							"UTF-8"));
			
			String line = null;

			while ((line = bufreader.readLine()) != null) {
				Log.i("line", line);
				result += line;
			}

			return result;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	public String signupJSP(String url, String msg) {
		String result = "";

		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		HttpEntity httpEntity = null;

		try {
			String msgdata[] = msg.trim().split(",");

			Log.i("msgdata[]", msgdata[0]);
			Log.i("msgdata[]", msgdata[1]);
			Log.i("msgdata[]", msgdata[2]);
			Log.i("msgdata[]", msgdata[3]);
			Log.i("msgdata[]", msgdata[4]);
			Log.i("msgdata[]", msgdata[5]);
			Log.i("msgdata[]", msgdata[6]);
			Log.i("msgdata[]", msgdata[7]);

			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			
			nameValuePairs.add(new BasicNameValuePair("id", msgdata[0]));
			nameValuePairs.add(new BasicNameValuePair("pw", msgdata[1]));
			nameValuePairs.add(new BasicNameValuePair("name", msgdata[2]));
			nameValuePairs.add(new BasicNameValuePair("nickname", msgdata[3]));
			nameValuePairs.add(new BasicNameValuePair("telnumber", msgdata[4]));
			nameValuePairs.add(new BasicNameValuePair("comment", msgdata[5]));
			nameValuePairs.add(new BasicNameValuePair("stdate", msgdata[6]));
			nameValuePairs.add(new BasicNameValuePair("modate", msgdata[7]));
			
			post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			HttpResponse response = client.execute(post);

			httpEntity = response.getEntity();
			result = new String(EntityUtils.toString(httpEntity).getBytes(
					"ISO-8859-1"), "UTF-8").trim();
			Log.i("result", result);
			/*
			BufferedReader bufreader = new BufferedReader(
					new InputStreamReader(response.getEntity().getContent(),
							"UTF-8"));

			String line = null;

			while ((line = bufreader.readLine()) != null) {
				Log.i("line", line);
				result += line;
			}
*/
			return result;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	public String picJSP(String url, String msg, String photopath) {
		String result = "";

		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);

		try {
			String msgdata[] = msg.split(",");

			Log.i("msgdata[]", msgdata[0]);
			Log.i("msgdata[]", msgdata[1]);
			Log.i("msgdata[]", msgdata[2]);
			Log.i("msgdata[]", msgdata[3]);
			Log.i("msgdata[]", msgdata[4]);
			Log.i("msgdata[]", msgdata[5]);
			Log.i("msgdata[]", msgdata[6]);
			Log.i("msgdata[]", msgdata[7]);
			Log.i("msgdata[]", msgdata[8]);
			Log.i("msgdata[]", msgdata[9]);
			Log.i("msgdata[]", msgdata[10]);
			Log.i("msgdata[]", msgdata[11]);

			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			
			nameValuePairs.add(new BasicNameValuePair("itemidx", msgdata[0]));
			nameValuePairs.add(new BasicNameValuePair("bizcd", msgdata[1]));
			nameValuePairs.add(new BasicNameValuePair("subjectcd", msgdata[2]));
			nameValuePairs.add(new BasicNameValuePair("locgpslati", msgdata[3]));
			nameValuePairs.add(new BasicNameValuePair("locgpslong", msgdata[4]));
			nameValuePairs.add(new BasicNameValuePair("locaddr1", msgdata[5]));
			nameValuePairs.add(new BasicNameValuePair("locaddr2", msgdata[6]));
			nameValuePairs.add(new BasicNameValuePair("telnumber", msgdata[7]));
			nameValuePairs.add(new BasicNameValuePair("comment", msgdata[8]));
			nameValuePairs.add(new BasicNameValuePair("photopath", msgdata[9]));
			nameValuePairs.add(new BasicNameValuePair("useyn", msgdata[10]));
			nameValuePairs.add(new BasicNameValuePair("sdate", msgdata[11]));

			post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			HttpResponse response = client.execute(post);

			BufferedReader bufreader = new BufferedReader(
					new InputStreamReader(response.getEntity().getContent(),
							"UTF-8"));

			String line = null;

			while ((line = bufreader.readLine()) != null) {
				Log.i("line", line);
				result += line;
			}

			return result;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	public String getJSP(String url) {
		String result = "";

		HttpClient client = new DefaultHttpClient();
		HttpEntity httpEntity = null;

		try {
			HttpPost post = new HttpPost(url);

			Log.i("URL", url);

			HttpParams param = client.getParams();
			HttpConnectionParams.setConnectionTimeout(param, 3000);
			HttpConnectionParams.setSoTimeout(param, 3000);

			HttpResponse response = client.execute(post);

			httpEntity = response.getEntity();
			result = new String(EntityUtils.toString(httpEntity).getBytes(
					"ISO-8859-1"), "UTF-8");

			return result;
		} catch (Exception e) {
			e.printStackTrace();
			client.getConnectionManager().shutdown();
			return "";
		}
	}
}